package com.dudu.service.impl;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dudu.access.ChengQiUtil;
import com.dudu.access.ImsUtils;
import com.dudu.access.RongLianUtils;
import com.dudu.access.TianYiUtil;
import com.dudu.access.YonYouUtil;
import com.dudu.bean.MeetInfoBean;
import com.dudu.enumeration.AbilityTypeEnum;
import com.dudu.service.IConferenceDialEndService;
import com.dudu.service.IMeetInfoService;

import core.CoreDao;
import core.CoreDaoInvoke;

/**
 * 用于一对一通话业务--结束通话
 * 
 * @author weidong
 * 
 */
public class ConferenceDialEndServiceImpl implements IConferenceDialEndService {
    // 用于查询数据用
    private CoreDao coreDao = CoreDaoInvoke.getInstance();
    private static final Logger log = core.util.Logger.getLogger(ConferenceDialEndServiceImpl.class.getSimpleName());

    /**
     * 用于结束一对一通话
     * 
     * @param 会话标识
     * @return
     */
    @Override
    public JSONObject doDialEnd(String sessionId) {
	log.info("ConferenceDialEndServiceImpl.doDialEnd 结束点击拨号");
	// 用于最后返回值
	JSONObject object = new JSONObject();
	try {
	    IMeetInfoService meetInfoService = new MeetInfoServiceImpl();
	    // 查询会议详情信息-根据会议sessionId-用于判断此通话的通话类型是哪个通道接入
	    MeetInfoBean meetInfo = meetInfoService.queryMeetImsBySessionId(coreDao, sessionId);
	    String abilityType = "";
	    if (meetInfo != null) {
		// 获取通话类型
		abilityType = meetInfo.getAbility_type();
		if (AbilityTypeEnum.DIAL_IMS.getKey().equals(abilityType)) {
		    // 结束IMS双人会议
		    object = EndDial(sessionId, abilityType, null);
		} else if (AbilityTypeEnum.DiAl_RONGLIAN.getKey().equals(abilityType)) {
		    // 结束容联双人会议
		    object = EndDial(sessionId, abilityType, null);
		} else if (AbilityTypeEnum.DIAL_TIANYI.getKey().equals(abilityType)) {
		    // 结束天亿双人会议
		    object = EndDial(sessionId, abilityType, null);
		} else if (AbilityTypeEnum.DIAL_CHENGQI.getKey().equals(abilityType)) {
		    // 结束承启通双人会议
		    object = EndDial(sessionId, abilityType, meetInfo.getChargeNumber());
		} else if (AbilityTypeEnum.DIAL_YONYOU.getKey().equals(abilityType)) {
		    // 结束承启通双人会议
		    object = EndDial(sessionId, abilityType, null);
		} else {
		    object.put("result", "-1");
		    object.put("errorMsg", "未配置对应通道！voice_type:" + abilityType);
		    object.put("describe", "未配置线路");
		    object.put("code", "0010");
		}
	    } else {
		object.put("result", "-1");
		object.put("describe", "sessionId不存在");
		object.put("code", "0006");
	    }
	} catch (Exception e) {
	    object.put("result", "-1");
	    object.put("errorMsg", "错误=" + e);
	    object.put("describe", "结束两方通话失败");
	    object.put("code", "0402");
	}

	return object;
    }

    private JSONObject EndDial(String sessionId, String abilityType, String displayNumber) {
	// 用于后面返回值
	JSONObject object = new JSONObject();
	try {
	    if (AbilityTypeEnum.DIAL_IMS.getKey().equals(abilityType)) {
		// IMS
		object = ImsUtils.talksV2AllStop(sessionId, false);
	    } else if (AbilityTypeEnum.DiAl_RONGLIAN.getKey().equals(abilityType)) {
		// 容联
		object = RongLianUtils.dialStop(sessionId);
	    } else if (AbilityTypeEnum.DIAL_TIANYI.getKey().equals(abilityType)) {
		// 天亿
		object = TianYiUtil.dialStop(sessionId);
	    } else if (AbilityTypeEnum.DIAL_CHENGQI.getKey().equals(abilityType)) {
		// 承启通
		object = ChengQiUtil.dialStop(sessionId, displayNumber);
	    } else if (AbilityTypeEnum.DIAL_YONYOU.getKey().equals(abilityType)) {
		// 天亿
		object = YonYouUtil.dialStop(sessionId);
	    } else {
		object.put("result", "-1");
		object.put("errorMsg", "未配置对应通道！voice_type:" + abilityType);
		object.put("describe", "未配置线路");
		object.put("code", "0010");
	    }

	    if (!"0".equals(object.get("result"))) {
		object.put("result", "-1");
		object.put("describe", "结束两方通话失败");
		object.put("code", "0402");
	    } else {
		object.put("result", "0");
		object.put("describe", "success");
		object.put("code", "0000");
	    }
	} catch (Exception e) {
	    object.put("result", "-1");
	    object.put("errorMsg", "会议结束失败" + e);
	    object.put("describe", "结束两方通话失败");
	    object.put("code", "0402");
	}
	return object;
    }

}
